package com.uit.gis.pojo;

import com.uit.gis.util.JsonData;
import com.uit.gis.util.Util.SpatialType;

import processing.data.JSONArray;
import processing.data.JSONObject;

public class Line extends Shape {

	private static final long serialVersionUID = 1L;
	JSONArray arrayJson = new JSONArray();

	@Override
	public void setup()
	{
		spatialType = SpatialType.LINES;
	}
	@Override
	public void draw() {

		if (!continueDraw)
			return;
		background(255);
		stroke(0);
		noFill();
		beginShape();
		for (int i = 0; i < arrayJson.size(); i++) {
			JSONObject vertex = arrayJson.getJSONObject(i);
			vertex(vertex.getInt("x"), vertex.getInt("y"));
		}
		endShape();
		// show to next segment to be added
		if (arrayJson.size() >= 1) {
			stroke(255);
			JSONObject vertex = arrayJson.getJSONObject(arrayJson.size() - 1);
			line(mouseX, mouseY, vertex.getInt("x"), vertex.getInt("y"));
		}
	}

	@Override
	public void mousePressed() {
		if (!continueDraw)
			return;
		JSONObject vertex = new JSONObject();
		vertex.setInt("x", mouseX);
		vertex.setInt("y", mouseY);
		arrayJson.append(vertex);
	}

	@Override
	public void mouseClicked(processing.event.MouseEvent e) {
		if (e.getButton() == processing.core.PConstants.RIGHT) {
			//saveJSONArray(arrayJson, "lines.json");
			saveToTextFile();
			continueDraw = false;
		}
	}

	@Override
	public void saveToTextFile() {
		// TODO Auto-generated method stub
		saveJSONArray(arrayJson, getSavingPath());
		super.saveToTextFile();
	};

	@Override
	public JsonData loadDataFromFile() {
		// TODO Auto-generated method stub
		JSONArray jsonArray = loadJSONArray(getSavingPath());
		JsonData jsonData = new JsonData(jsonArray, null);
		return jsonData;
	};
}
